package com.wmx.mp.controller;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.wmx.mp.entity.Dept;
import com.wmx.mp.service.IDeptService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门信息前端控制器
 * </p>
 *
 * @author 汪茂雄
 * @since 2023-04-01
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Resource
    private IDeptService deptService;

    /**
     * http://localhost:8080/dept/pageList?page=1&count=10
     *
     * @param page  ：查询页码，从1开始
     * @param count ：每页展示的条数
     * @return
     */
    @GetMapping("/pageList")
    public IPage<Dept> pageList(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer count) {
        Page<Dept> pageDTO = PageDTO.of(page, count);
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        Page<Dept> deptPage = deptService.page(pageDTO, queryWrapper);
        return deptPage;
    }

    /**
     * 批量新增保存。
     * 插入操作时：如果属性没有指定，或者指定值为null，则属性名称不会出现在 insert into sql中。
     * http://localhost:8080/basUser/saveList
     * <pre>
     *     [
     *     {
     *         "loc": "北京",
     *         "name": "预算部门"
     *     },
     *     {
     *         "loc": "长沙",
     *         "name": "支付部门"
     *     }
     * ]
     * </pre>
     *
     * @param deptList ：待新增保存的数据
     * @return
     */
    @PostMapping("/saveList")
    public Map<String, String> saveList(@RequestBody List<Dept> deptList) {
        /**
         * <pre>
         * ==> Preparing: INSERT INTO DEPT ( dept_no, NAME, LOC, CREATE_TIME, UPDATE_TIME ) VALUES ( ?, ?, ?, ?, ? )
         * ==> Parameters: -754937854(Integer), 预算部门2(String), 北京2(String), 2023-04-01 13:27:02.692(Timestamp), 2023-04-01 13:27:02.692(Timestamp)
         * </pre>
         */
        deptService.saveBatch(deptList);
        Map<String, String> resultMap = MapUtil.builder("code", "200").put("msg", "操作成功").build();
        return resultMap;
    }

}
